package com.yyge.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

public class JdbcConfig {
  @Value("${jdbc.driverClass}")
  private String driverClassName;

  @Value("${jdbc.url}")
  private String url;

  @Value("${jdbc.username}")
  private  String username;

  @Value("${jdbc.password}")
  private String password;

    // 将druid数据源连接池的对象托管到IOC容器中
    // @Bean注解一般用于导入第三方的bean，交给IOC容器去管理
    // @Bean的名字默认根据方法名定义
  @Bean
  public DataSource dataSource(){
    DruidDataSource dataSource =new DruidDataSource();
    dataSource.setDriverClassName(this.driverClassName);
    dataSource.setUrl(this.url);
    dataSource.setUsername(this.username);
    dataSource.setPassword(this.password);
    return dataSource;
  }

  //配置JDBC事务管理器
  //只要是进行事务操作,就必须要指定事务管理器
  @Bean
  public PlatformTransactionManager transactionManager(DataSource dataSource){
    DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(dataSource);
    return dataSourceTransactionManager;
  }

}
